package cn.iocoder.yudao.module.ao.service.academicyear;

import cn.hutool.core.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ao.controller.admin.academicyear.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.academicyear.AcademicYearDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ao.dal.mysql.academicyear.AcademicYearMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.ao.enums.ErrorCodeConstants.*;

/**
 * 学年及班级信息 Service 实现类
 *
 * @author 智教云枢
 */
@Service
@Validated
@Slf4j
public class AcademicYearServiceImpl implements AcademicYearService {

    @Resource
    private AcademicYearMapper academicYearMapper;

    @Override
    public Long createAcademicYear(AcademicYearSaveReqVO createReqVO) {
        // 插入
        AcademicYearDO academicYear = BeanUtils.toBean(createReqVO, AcademicYearDO.class);
        academicYearMapper.insert(academicYear);

        // 返回
        return academicYear.getYearId();
    }

    @Override
    public void updateAcademicYear(AcademicYearSaveReqVO updateReqVO) {
        // 校验存在
        validateAcademicYearExists(updateReqVO.getYearId());
        // 更新
        AcademicYearDO updateObj = BeanUtils.toBean(updateReqVO, AcademicYearDO.class);
        academicYearMapper.updateById(updateObj);
    }

    @Override
    public void deleteAcademicYear(Long id) {
        // 校验存在
        validateAcademicYearExists(id);
        // 删除
        academicYearMapper.deleteById(id);
    }

    @Override
        public void deleteAcademicYearListByIds(List<Long> ids) {
        // 删除
        academicYearMapper.deleteByIds(ids);
        }


    private void validateAcademicYearExists(Long id) {
        if (academicYearMapper.selectById(id) == null) {
            throw exception(YEAR_NOT_EXISTS);
        }
    }

    @Override
    public AcademicYearDO getAcademicYear(Long id) {
        return academicYearMapper.selectById(id);
    }

    @Override
    public PageResult<AcademicYearDO> getAcademicYearPage(AcademicYearPageReqVO pageReqVO) {
        return academicYearMapper.selectPage(pageReqVO);
    }

}